﻿<UserControl x:Class="Ags.MonsterTools.PingTool.PingView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             xmlns:common="http://ags.monstertools/common"
             xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:pingTool="clr-namespace:Ags.MonsterTools.PingTool"
             xmlns:tb="http://www.hardcodet.net/taskbar"
             d:DesignHeight="480" d:DesignWidth="640" 
             Background="White"
             MinWidth="640">
    <UserControl.Resources>
        <pingTool:RoundtripTimeRedConverter x:Key="RedConverter"/>
        <pingTool:RoundtripTimeOrangeConverter x:Key="OrangeConverter"></pingTool:RoundtripTimeOrangeConverter>
        <pingTool:RoundtripTimeYellowConverter x:Key="YellowConverter"></pingTool:RoundtripTimeYellowConverter>
        <pingTool:TimeConverter x:Key="TimeConverter"></pingTool:TimeConverter>
        <pingTool:MilisecondsToSecondsConverter x:Key="SecondsConverter"></pingTool:MilisecondsToSecondsConverter>
        <pingTool:BoolInverterConverter x:Key="Inverter"></pingTool:BoolInverterConverter>
        <pingTool:IpAddressConverter x:Key="IpAddressConverter"></pingTool:IpAddressConverter>
        <pingTool:RoundtripTimeGreenConverter x:Key="GreenConverter"></pingTool:RoundtripTimeGreenConverter>

        <Style x:Key="BtnStyle" TargetType="Button">
            <Setter Property="Margin" Value="5,0,5,0"></Setter>
            <Setter Property="Width" Value="60"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter Name="ContentImage"  ContentTemplate="{TemplateBinding Content}"/>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="ContentImage" Property="Opacity" Value="0.5"></Setter>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="ContentImage" Property="Opacity" Value="0.9"></Setter>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="ContentImage" Property="Opacity" Value="0.2"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="LblSecondsStyle" TargetType="Label">
            <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
            <Setter Property="FontSize" Value="14"></Setter>
            <Setter Property="FontWeight" Value="SemiBold"></Setter>
        </Style>
        <Style x:Key="LblStyle" TargetType="Label">
            <Setter Property="FontWeight" Value="SemiBold"></Setter>
            <Setter Property="Margin" Value="0,5,0,5"></Setter>
        </Style>
        <Style x:Key="TbStyle" TargetType="TextBox">
            <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
            <Setter Property="Margin" Value="5,0,5,0"></Setter>
            <Setter Property="Height" Value="25"></Setter>
        </Style>
        <Style x:Key="ListBoxStyle" TargetType="ListBox">
            <Setter Property="Margin" Value="5,0,5,0"></Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border CornerRadius="10" BorderThickness="1" BorderBrush="Gainsboro" 
                                            Margin="3"
                                            Background="LightGreen" Name="itemBorder">
                            <Grid Margin="5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                </Grid.ColumnDefinitions>

                                <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Width="27" Height="27"
                                       Source="/Ags.MonsterTools.PingTool;component/Images/ping.png"></Image>

                                <Label Content="IP" Grid.Column="1" Grid.Row="0"></Label>
                                <Label Content="{Binding Path=., Converter={StaticResource IpAddressConverter}}" Grid.Column="2" Grid.Row="0"></Label>

                                <Label Content="Status" Grid.Column="1" Grid.Row="1"></Label>
                                <Label Content="{Binding Path=Result.Status}" Grid.Row="1" Grid.Column="2"></Label>

                                <Label Grid.Column="3" Grid.Row="1" Content="Timestamp"></Label>
                                <Label Grid.Column="4" Grid.Row="1" Content="{Binding Path=Time, Converter={StaticResource TimeConverter}}"></Label>

                                <Label Content="Response time" Grid.Row="0" Grid.Column="5"></Label>
                                <Label Content="{Binding Path=Result.RoundtripTime}" 
                                       FontSize="32" 
                                       Grid.RowSpan="2" 
                                       Grid.Row="0"
                                       Grid.Column="6"></Label>
                                <Label Content="ms" Grid.Row="1" Grid.Column="7"></Label>
                            </Grid>
                        </Border>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=Result, Converter={StaticResource RedConverter}}" Value="true">
                                <Setter Property="Background" Value="Crimson"  TargetName="itemBorder" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=Result, Converter={StaticResource OrangeConverter}}" Value="true" >
                                <Setter Property="Background" Value="DarkOrange" TargetName="itemBorder" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=Result, Converter={StaticResource YellowConverter}}" Value="true">
                                <Setter Property="Background" Value="Yellow"  TargetName="itemBorder" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <!--NotifyIcon-->
        <tb:TaskbarIcon DoubleClickCommand="{Binding ShowParentCommand}">
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Show" ToolTip="Show MonsterTools Window" Command="{Binding ShowParentCommand}">
                        <MenuItem.Icon>
                            <Image Source="Images/show.ico" Width="16" Height="16"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Hide" ToolTip="Hide MonsterTools Window" Command="{Binding HideParentCommand}">
                        <MenuItem.Icon>
                            <Image Source="Images/hide.ico" Width="16" Height="16"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
            <tb:TaskbarIcon.Style>
                <Style>
                    <Setter Property="tb:TaskbarIcon.ToolTipText" Value="{Binding Path=LastReply.Result.RoundtripTime}"></Setter>
                    <Setter Property="tb:TaskbarIcon.IconSource" Value="Images/gray.ico"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=LastReply.Result, Converter={StaticResource GreenConverter}, UpdateSourceTrigger=PropertyChanged}" Value="true">
                            <Setter Property="tb:TaskbarIcon.IconSource" Value="Images/green.ico"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=LastReply.Result, Converter={StaticResource RedConverter}, UpdateSourceTrigger=PropertyChanged}" Value="true">
                            <Setter Property="tb:TaskbarIcon.IconSource" Value="Images/red.ico"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=LastReply.Result, Converter={StaticResource YellowConverter}, UpdateSourceTrigger=PropertyChanged}" Value="true">
                            <Setter Property="tb:TaskbarIcon.IconSource" Value="Images/yellow.ico"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=LastReply.Result, Converter={StaticResource OrangeConverter}, UpdateSourceTrigger=PropertyChanged}" Value="true">
                            <Setter Property="tb:TaskbarIcon.IconSource" Value="Images/orange.ico"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </tb:TaskbarIcon.Style>
        </tb:TaskbarIcon>
        <!-- Content -->
        <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
            <Button Command="{Binding PingCommand}" Style="{StaticResource BtnStyle}" ToolTip="Start ping"
                    IsEnabled="{Binding InputEnabled}">
                <Image Source="Images/Play.png" Width="32" Height="32"></Image>
            </Button>
            <Button Command="{Binding StopCommand}" Style="{StaticResource BtnStyle}" ToolTip="Stop ping"
                    IsEnabled="{Binding InputEnabled, Converter={StaticResource Inverter}}">
                <Image Source="Images/Stop.png" Width="32" Height="32"></Image>
            </Button>
            <Button Command="{Binding ClearCommand}" Style="{StaticResource BtnStyle}" ToolTip="Clear list">
                <Image Source="Images/Trash.png" Width="32" Height="32"></Image>
            </Button>
        </StackPanel>
        <!--Parameters-->
        <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0">
            <StackPanel Orientation="Vertical">
                <Label Content="URL" Style="{StaticResource LblStyle}"></Label>
                <TextBox Width="180" 
                         Style="{StaticResource TbStyle}" 
                         Text="{Binding Host, FallbackValue='www.google.de', ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                         IsEnabled="{Binding InputEnabled}"
                         >
                    <TextBox.InputBindings>
                        <KeyBinding Command="{Binding Path=PingCommand}" Key="Enter" />
                    </TextBox.InputBindings>
                </TextBox>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="10,0,3,0">
                <Label Content="TIMEOUT" Style="{StaticResource LblStyle}"></Label>
                <Slider Value="{Binding Timeout}"
                        Maximum="10000"
                        Minimum="1000"
                        Interval="1000"
                        Width="100"
                        IsEnabled="{Binding InputEnabled}"
                        Name="TimeoutSlider"
                        ToolTip="Maximum timeout for ping response in seconds"></Slider>
                <Label Content="{Binding ElementName=TimeoutSlider, Path=Value, Converter={StaticResource SecondsConverter}}"
                       Style="{StaticResource LblSecondsStyle}"></Label>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="10,0,3,0">
                <Label Content="INTERVAL" Style="{StaticResource LblStyle}"></Label>
                <Slider Value="{Binding Delay}"
                        Maximum="10000"
                        Minimum="1000"
                        Width="100"
                        Interval="1000"
                        IsEnabled="{Binding InputEnabled}"
                        Name="DelaySlider"
                        ToolTip="Delay between two pings in seconds"></Slider>
                <Label Content="{Binding ElementName=DelaySlider, Path=Value, Converter={StaticResource SecondsConverter}}"
                       Style="{StaticResource LblSecondsStyle}"></Label>
            </StackPanel>
        </StackPanel>
        <!--Status-->
        <TextBlock Text="{Binding Info}" Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0"></TextBlock>
        <!--Output-->
        <ListBox common:ItemsSourceBehavior.ItemsSource="{Binding ReplyList}"
                 Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="0" 
                 Style="{StaticResource ListBoxStyle}"
                 x:Name="ResultListBox">
            <common:ItemsSourceBehavior.FadeInAnimation>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity"
                             From="0.0"
                             To="1.0"
                             Duration="0:0:1"/>
                </Storyboard>
            </common:ItemsSourceBehavior.FadeInAnimation>
            <interactivity:Interaction.Behaviors>
                <common:AutoScrollToLastItemBehavior></common:AutoScrollToLastItemBehavior>
            </interactivity:Interaction.Behaviors>
        </ListBox>
    </Grid>
</UserControl>
